<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Promise中的静态方法</title>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.24.0/axios.js"></script>
<script>
/*
* Promise.all()
* */

/*只有数组中所有的Promise都resolve才会执行then 只要有一个reject就直接去执行catch then就不会执行了*/
/*Promise.all([
  new Promise(((resolve, reject) => { setTimeout(()=>{resolve(1000)},1000)})),
  new Promise(((resolve, reject) => { setTimeout(()=>{resolve(2000)},1500)})),
  new Promise(((resolve, reject) => { setTimeout(()=>{reject(3000)},2000)})),
  new Promise(((resolve, reject) => { setTimeout(()=>{reject(4000)},2500)}))
]).then(value => {
  console.log(value);
}).catch(reason => {
  console.log(reason);
})*/


/*
* Promise.all()中如果传递的不是Promise对象，可以被原路返回
*
* */

/*Promise.all([
  new Promise(((resolve, reject) => { setTimeout(()=>{resolve(1000)},1000)})),
  new Promise(((resolve, reject) => { setTimeout(()=>{resolve(2000)},1500)})),
  [1,2,3].map(num => num+1)
]).then(value => {
  console.log(value);
}).catch(reason => {
  console.log(reason);
})*/

/*
* Promise.all()适用场景 发起一组请求 符合全或无
* */
/*let urls =[
  "https://api.github.com/users/tom",
  "https://api.github.com/users/jack",
  "https://api.github.com/users/lily",
]
Promise.all(urls.map(url => axios.get(url)))
  .then(values =>{
     values.forEach(console.log)
  }).catch(reason => {
  console.log(reason);
})*/

/*
let urls1 =[
  "https://api.github.com/users/tom",
  "https://api.github.com/users/jack",
  "https://api.github1.com/user",
]
Promise.allSettled(urls1.map(url => axios.get(url)))
       .then(values =>{
         values.forEach(console.log)
       }).catch(reason => {
  reason.forEach(console.log)
})
*/
/*Polyfill*/
Promise.allSettled1 = function(promises) {
  /*注意这里箭头函数直接返回一个对象要加括号*/
  const handleFulfilled = value=>({status:'fulfilled',value})
  const handleRejected = reason=>({status:'rejected',reason})

  const convertedPromises = promises.map(promise =>Promise.resolve(promise).then(handleFulfilled,handleRejected))
  return Promise.all(convertedPromises)
}



</script>
</body>
</html>